/*
 * Balancer.h
 *
 *  Created on: 28/06/2014
 *      Author: Caina
 */

#ifndef BALANCER_H_
#define BALANCER_H_

#include <cstdio>
#include <cmath>
#include <map>
#include <queue>
#include <vector>

#include "TCPSocket.h"
#include "Globals.h"
#include "MessageTypes.h"
#include "SDL2/SDL.h"
#include "User.h"
#include "ByteQueue.h"
#include "File.h"
#include "FileTable.h"
#include "FileDescriptor.h"
#include "Order.h"

class Balancer {
private:
	Balancer();
	virtual ~Balancer();

	std::queue<Order> orderQueue;
	std::map<unsigned int, int> file_count;
public:
	enum EventType {
		NEW_NODE, NODE_LOSS, FILE_DELETION
	};
	static Balancer& get_instance();
	static unsigned int get_manager_ip();
	//void update_manager_ip();
	void run(EventType, unsigned int = 0);
	static std::string convert_ip_to_string(unsigned int);
	void send_message(Order);
	int get_average();
	static void broadcast_to_peers(const std::vector<char>& message,
			bool including_myself = true);
	void print_file_count();
	void send_updated_file_table();
	void update_file_count();
	void new_node();
	void node_loss(unsigned int);
	void file_deletion();
};

#endif /* BALANCER_H_ */
